package com.zjzywl.smoke.controller;

import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

import com.zjzywl.smoke.config.AppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zjzywl.smoke.entity.WorkOrder;
import com.zjzywl.smoke.util.WebResult;

@Controller
@RequestMapping("/workOrder")
public class WorkOrderController extends BaseController {
    @Autowired
    private AppConfig appConfig;

    /*
     * 添加,修改任务单
     */
    @PostMapping(value = "/save")
    @ResponseBody
    public WebResult add(@RequestBody WorkOrder workOrder, @PathParam("orgid") Integer orgid) {
        ResponseEntity<WorkOrder> history = appConfig.restTemplate()
                .postForEntity(appConfig.getGetDataIp() + "/workOrder/save?orgid=" + orgid, workOrder, WorkOrder.class);
        return WebResult.OK(history);
    }
    /*
     * 添加,修改apk任务单
     */
    @PostMapping(value = "/apkSave")
    @ResponseBody
    public WebResult apkSave(@RequestBody WorkOrder workOrder, @PathParam("orgid") Integer orgid) {
        ResponseEntity<WorkOrder> history = appConfig.restTemplate()
                .postForEntity(appConfig.getGetDataIp() + "/workOrder/apkSave?orgid=" + orgid, workOrder, WorkOrder.class);
        return WebResult.OK(history);
    }

    /*
     * 获取任务单历史
     */
    @GetMapping(value = "/getHistory")
    @ResponseBody
    public WebResult getHistory(HttpServletRequest request) throws Exception {
        String userId = this.getUserId();
        ResponseEntity<Object> history = appConfig.restTemplate().getForEntity(appConfig.getGetDataIp()
                + "/workOrder/getHistory?userId=" + userId + "&" + URLDecoder.decode(request.getQueryString(), "utf-8"),
                Object.class);

        return WebResult.OK(history);
    }

    /*
     * 获取目标点周边的消防栓设备
     */
    @GetMapping(value = "/getPeripheralEquipment")
    @ResponseBody
    public WebResult getPeripheralEquipment(

            @RequestParam(value = "devCode", required = false) String devCode) throws Exception {
        String userId = this.getUserId();
        ResponseEntity<Object> history = appConfig.restTemplate().getForEntity(
                appConfig.getGetDataIp() + "/workOrder/getPeripheralEquipment?userId=" + userId + "&devCode=" + devCode,
                Object.class);

        return WebResult.OK(history);
    }

    /*
     * 修改任务单状态
     * 
     * 
     */
    @GetMapping(value = "/putTaskState")
    @ResponseBody
    public WebResult putTaskState(@RequestParam(value = "workOrderId", required = false) Integer workOrderId,
            @RequestParam(value = "taskState", required = false) String taskState) throws Exception {
        ResponseEntity<Object> history = appConfig.restTemplate().getForEntity(appConfig.getGetDataIp()
                + "/workOrder/putTaskState?workOrderId=" + workOrderId + "&taskState=" + taskState, Object.class);

        return WebResult.OK(history);
    }

    /*
     * 查询单个任务单记录
     */
    @GetMapping(value = "/getWorkOrder")
    @ResponseBody
    public WebResult getWorkOrder(@RequestParam(value = "workOrderId", required = false) Integer workOrderId)
            throws Exception {
        ResponseEntity<Object> history = appConfig.restTemplate().getForEntity(
                appConfig.getGetDataIp() + "/workOrder/getWorkOrder?workOrderId=" + workOrderId, Object.class);
        return WebResult.OK(history);
    }
}